package com.ljk.util.pdf.template;

import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.ljk.util.pdf.PdfUtil;
import com.ljk.util.pdf.data.BasePresTemplate;
import com.ljk.util.pdf.data.PrescriptionItem;
import com.ljk.util.pdf.data.WesternPrescription;
import com.ljk.util.string.StringUtil;

import java.util.*;

import static com.ljk.util.pdf.PdfUtil.createCell;

public class PdfTemplate4Xy extends BasePresTemplate<WesternPrescription> {

    @Override
    public int convertItem(BaseFont baseFont, WesternPrescription pres, PdfPTable presTable, float[] leftRightWidth) {
        Font font = new Font(baseFont, 11, Font.NORMAL);
        Integer groupNo = null;
        ArrayList<List<PrescriptionItem>> groupList = new ArrayList<>();
        for (PrescriptionItem drug : pres.getPresList()) {
            if (drug.getGroupNo() == null) {
                groupList.add(Collections.singletonList(drug));
            } else {
                if (Objects.equals(groupNo, drug.getGroupNo())) {
                    groupList.get(groupList.size() - 1).add(drug);
                } else {
                    ArrayList<PrescriptionItem> innerList = new ArrayList<>();
                    innerList.add(drug);
                    groupList.add(innerList);
                }
            }
            groupNo = drug.getGroupNo();
        }
        PdfPCell leftCell = PdfUtil.createBankCell();
        for (List<PrescriptionItem> drugList : groupList) {
            if (drugList.size() == 1) {
                PrescriptionItem drug = drugList.get(0);

                String drugName = joinParam("\n", drug.getItemName(), StringUtil.isEmpty(drug.getRemark()) ? null : "备注：" + drug.getRemark());

                String specLine2;
                String specLine3 = null;
//                drug.getUsageType(), drug.getFrequencyName(), medicationDay
                String doseAndUnit = StringUtil.nvl(drug.getDose(), "") + StringUtil.nvl(drug.getDoseUnit(), "");
                String usageType = StringUtil.nvl(drug.getUsageType(), "");
                String frequencyName = StringUtil.nvl(drug.getFrequencyName(), "");

                if (drug.getGroupNo() != null) {//有组号的
                    //单次剂量+用法\n频次+天数
                    specLine2 = doseAndUnit + " " + usageType;
                    String medicationDay = drug.getMedicationDay() == null ? null : drug.getMedicationDay() + "天";
                    specLine3 = frequencyName + " " + medicationDay;
                } else {//无组号的
                    //单次剂量+用法+频次
                    specLine2 = doseAndUnit + " " + usageType + " " + frequencyName;
                }
                String spec = joinParam("\n", drug.getSpec(), specLine2, specLine3);


                PdfPCell oneDrugCell = oneDrugCell(font, drugName, spec, drug.getQuantity(), drug.getQuantityUnit(), "", "");
                presTable.addCell(PdfUtil.parseOneCell(leftRightWidth, leftCell, oneDrugCell));
                continue;
            }
            for (int i = 0; i < drugList.size(); i++) {
                PrescriptionItem drug = drugList.get(i);

                String drugName = joinParam("\n", drug.getItemName(), StringUtil.isEmpty(drug.getRemark()) ? null : "备注：" + drug.getRemark());
                String doseAndUnit = StringUtil.nvl(drug.getDose(), "") + StringUtil.nvl(drug.getDoseUnit(), "");
                String spec = joinParam("\n", drug.getSpec(), doseAndUnit);
                String usage = "";
                String groupStr;
                if (i == 0) {
                    groupStr = "┓";
                    String medicationDay = drug.getMedicationDay() == null ? null : drug.getMedicationDay() + "天";
                    usage = joinParam("\n", drug.getUsageType(), drug.getFrequencyName(), medicationDay);
                } else if (i == drugList.size() - 1) {
                    groupStr = "┛";
                } else {
                    groupStr = "┃";
                }
                PdfPCell oneDrugCell = oneDrugCell(font, drugName, spec, drug.getQuantity(), drug.getQuantityUnit(), usage, groupStr);
                presTable.addCell(PdfUtil.parseOneCell(leftRightWidth, leftCell, oneDrugCell));
            }
        }
        String remark = pres.getRemark();
        if (StringUtil.isEmpty(remark)) {
            remark = "无";
        }
        presTable.addCell(PdfUtil.parseOneCell(leftRightWidth, leftCell, createCell(font, "备注：" + remark, Element.ALIGN_LEFT)));

        return pres.getPresList().size();
    }

    /**
     * 一个药品的展示
     */
    private static PdfPCell oneDrugCell(Font font, String drugName, String spec, Integer quantity, String quantityUnitStr, String usage, String groupStr) {
        PdfPCell cell = createCell(font, drugName, Element.ALIGN_TOP, Element.ALIGN_LEFT);
        PdfPCell cell1 = createCell(font, spec, Element.ALIGN_TOP, Element.ALIGN_LEFT);
        PdfPCell cell2 = createCell(font, quantity == null ? "" : quantity.toString(), Element.ALIGN_CENTER);
        PdfPCell cell3 = createCell(font, quantityUnitStr, Element.ALIGN_CENTER);
        //        ┓┃┛      ┐│┘     ┒┚     ┑┙
        PdfPCell cell4 = createCell(font, groupStr, Element.ALIGN_LEFT);
        PdfPCell cell5 = createCell(font, usage, Element.ALIGN_CENTER);
        List<PdfPCell> list = Arrays.asList(cell, cell1, cell2, cell3, cell4, cell5);
        return PdfUtil.parseOneCell(new float[]{29, 15, 4, 3, 4, 11}, list);
    }


}
